package edu.neepu;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import jdk.internal.dynalink.beans.StaticClass;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URLConnection;
import java.util.ArrayList;

public class MainClass implements Runnable{
    int indexs = 0;
    public static void main(String arhs) {
        //我愿变成童话里你爱的那个天使，展开双手变成翅膀守护你----------
    }

    @Test
    public void test1() {
        System.out.println("你要相信相信我们会像童话故事里，幸福和快乐是结局");
        //也许你不会懂，我的天空，星星都亮了
        //我愿变成，童话里,你爱的那个天使
        //展开双手，变成翅膀守护你
        //你要相信，相信我们会像童话故事里
        //幸福和快乐是结局
        try {
            Document document = Jsoup.connect("http://www.baidu.com").get();
            System.out.println(document);
            System.out.println(document.getAllElements());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        String html = "<html><head><title>hhahahah</title></head>" +
                "<body><p>我是你爸爸</p></body></html>";
        Document document = Jsoup.parse(html);
        System.out.println(document);
    }

    @Test
    public void test3() throws IOException {
        File file = new File("D:\\software\\DSADASDSA\\src\\main\\resources\\1.html");
        Document document = Jsoup.parse(file, "utf-8");
        Elements p = document.getElementsByTag("p");
        System.out.println(document);
        System.out.println(p);
        for (Element element : p) {
            System.out.println(element.text());
        }
        String te1 = document.getElementById("1").text();
        String te2 = document.getElementsByClass("3").text();
        System.out.println("--------------------------");
        System.out.println(document.select("#1"));
        System.out.println();

    }

    @Test
    public void test4() throws IOException {

        Document document = Jsoup.connect("https://www.csdn.net/").get();
        //document.head().getElementsByTag("Title")
        Elements a = document.getElementsByTag("img");
        for (Element element : a) {
            System.out.println(element.attr("abs:src"));
        }

    }

    @Test
    public void test5() throws IOException {
        Document document = Jsoup.connect("https://www.zhihu.com/explore").get();
        Elements elementsByClass = document.getElementsByClass("zh-summary");
        Elements select = document.select(".zh-summary");
        System.out.println(select);
        System.out.println("--------");
        for (Element element : select) {
            System.out.println(element.text());
        }

    }

    @Test
    public void test6() throws IOException {
        Document document = Jsoup.connect("https://www.zhihu.com/explore/recommendations").get();
        Elements select = document.select(".zm-item");
        //System.out.println(select);
        for (Element element : select) {
            System.out.println("作者:" + element.select(".author-link").text());
            String href = element.select(".question_link").attr("abs:href");
            if (href == null || href.equals(""))
                break;
            System.out.println(href);
            System.out.println("回答");
            Document document1 = Jsoup.connect(href).get();
            Elements select1 = document1.getElementsByTag("span");
            Elements select2 = select1.select(".RichText");

            for (Element element1 : select2) {
                String p = element1.getElementsByTag("p").text();
                System.out.println(p);
            }

        }
    }

    @Test
    public void test7() throws IOException {
        for (int i = 1; i <= 10; i++) {
            String href = "https://store.steampowered.com/search/?specials=" + i;
            Document document = Jsoup.connect(href).get();
            Elements select = document.select(".page_content_ctn");
            Elements select1 = select.select(".search_result_row");
            for (Element element : select1) {
                String game_name = element.select(".title").text();
                String date = element.select(".col.search_released").text();
                String evaluate = element.select(".search_review_summary.mixed").attr("data-tooltip-html");
                Elements select2 = element.getElementsByTag("strike");
                String o_price = select2.text();
                Elements select3 = element.select(".col.search_price.discounted");
                String n_price = select3.text().replace(o_price, "");
                System.out.println("游戏名" + game_name);
                System.out.println("日期：" + date);
                System.out.println("评分：" + evaluate);
                System.out.println("原价：" + o_price);
                System.out.println("现价：" + n_price);
                System.out.println("------------------------");

            }
        }


    }

    public static  void main(String[] args){


    }
    public void test8(int index) throws IOException {
            Document document = Jsoup.connect("https://bing.ioliu.cn/?p=" + index+ 1).get();
            Elements select = document.select(".item");
            for (Element element : select) {
                //图片链接地址
                Elements imgsrc = element.select(".mark");
                String src = imgsrc.attr("abs:href");

                //图片下载地址
                Elements options = element.select(".options");
                Elements downloadHref = options.select(".ctrl.download");
                String imageDownloadHref = downloadHref.attr("abs:href");

                //获取图片信息
                Document imgView = Jsoup.connect(src).get();
                //日期
                Elements dateClass = imgView.select(".calendari");
                String date = dateClass.select(".t").text();
                date = date.replace(", "," ");
                //位置
                Elements locationClass =imgView.select(".location");
                String location = locationClass.select(".t").text();
                location = location.replace(", ","");
                System.out.println(date);
                System.out.println(location);
                System.out.println("-----------");

                //下载图片
                URL url = new URL(imageDownloadHref);
                // 打开连接
                URLConnection con = url.openConnection();
                // 输入流
                InputStream is = con.getInputStream();
                // 1K的数据缓冲
                byte[] bs = new byte[1024];
                // 读取到的数据长度
                int len;
                // 输出的文件流
                OutputStream os = new FileOutputStream("D:\\img\\" + date+ "-"+location + ".jpg");
                // 开始读取
                while ((len = is.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
                // 完毕，关闭所有链接
                os.close();
                is.close();

            }
    }

    public void run() {
        try {
            if(indexs<10) {
                test8(indexs);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
